package com.ys.service.wallet.btc;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ys.entity.WalletDeposit;
import com.ys.entity.WalletWithdraw;
import com.ys.entity.block.Transaction;
import com.ys.entity.block.btc.BtcAccountTransatcion;
import com.ys.entity.block.btc.BtcTransation;
import com.ys.enu.CurrencyEnum;
import com.ys.exception.base.BizException;
import com.ys.service.WalletDepositServiceImap;
import com.ys.service.WalletScanBlockServiceImap;
import com.ys.service.WalletWithdrawServiceImap;
import com.ys.service.wallet.AbstractWalletJobService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Slf4j
@Data
@Component
public class BtcService extends AbstractWalletJobService {

    @Autowired
    private WalletWithdrawServiceImap walletWithdrawServiceImap;
    @Autowired
    private BtcWalletService btcWalletService;
    @Autowired
    private WalletDepositServiceImap walletDepositServiceImap;
    @Autowired
    private WalletScanBlockServiceImap walletScanBlockServiceImap;
    @Override
    public Integer getCurrency() {
        return CurrencyEnum.BTC.currencyChainType;
    }

    @Override
    public void dealTransaction(Transaction t, WalletWithdraw walletWithdraw) {
        BtcTransation transaction= (BtcTransation) t;
        if(transaction==null || StrUtil.isBlank(transaction.getTxid()))
            return;
        WalletWithdraw updateBlockNum=new WalletWithdraw();

        updateBlockNum.setId(walletWithdraw.getId());
        updateBlockNum.setBlockHash(transaction.getBlockhash());
        /**设置确认数**/
        updateBlockNum.setBlockHeight(transaction.getConfirmations());
        /**查询区块号blocknum**/
        String blocknum=btcWalletService.queryBlockByHash(transaction.getBlockhash());
        if(StrUtil.isEmpty(blocknum)){
            log.warn("比特系根据区块hash查询区块号失败");
            return ;
        }
        updateBlockNum.setBlockNum(Integer.parseInt(blocknum));
        walletWithdrawServiceImap.updateById(updateBlockNum);
    }

    @Override
    public Transaction getTransaction() {
        return new BtcTransation();
    }

    @Override
    public void updateDepositWithdrawStatus(Integer currentBlockNum) {
        WalletDeposit walletDeposit=new WalletDeposit();
        walletDeposit.setCurrencyChainType(CurrencyEnum.BTC.getCurrencyChainType());
        walletDeposit.setBlockNum(currentBlockNum);
        int depositRow=walletDepositServiceImap.updateDepositStatus(walletDeposit,CurrencyEnum.BTC.getConfirmNum());
        log.info("比特链上此次冲提的冲币交易条数为 row="+depositRow);

        WalletWithdraw walletWithdraw=new WalletWithdraw();
        walletWithdraw.setCurrencyChainType(CurrencyEnum.BTC.getCurrencyChainType());
        walletWithdraw.setBlockNum(currentBlockNum);
        int withdrawRow=walletWithdrawServiceImap.updateWithdrawStatus(walletWithdraw,CurrencyEnum.BTC.getConfirmNum());
        log.info("比特链上此次更新的提币交易条数为 row="+withdrawRow);
    }

    /**
     * 处理充值交易
     * 处理
     * @param t 处理完成返回true 否则返回false
     */
    @Transactional
    public boolean dealDepositeTransaction(BtcAccountTransatcion btcAccountTransatcion) throws BizException {

        EntityWrapper<WalletDeposit> entityWrapper=new EntityWrapper<>();
        entityWrapper.eq("txHash",btcAccountTransatcion.getTxid());
        entityWrapper.eq("depositToAddress",btcAccountTransatcion.getAddress());

        if(CollUtil.isNotEmpty(walletDepositServiceImap.selectList(entityWrapper))){
            log.info("比特币充值记录已经入库：btcAccountTransatcion="+ JSONUtil.toJsonStr(btcAccountTransatcion));
            return true;
        }

        WalletDeposit walletDeposit=new WalletDeposit();
        walletDeposit.setBlockHash(btcAccountTransatcion.getBlockhash());
        walletDeposit.setDepositToAddress(btcAccountTransatcion.getAddress());
        walletDeposit.setTxHash(btcAccountTransatcion.getTxid());

        walletDeposit.setCurrency(CurrencyEnum.BTC.currency);
        walletDeposit.setCurrencyChainType(CurrencyEnum.BTC.currencyChainType);
        walletDeposit.setBlockHash(btcAccountTransatcion.getBlockhash());
        walletDeposit.setDepositAmount(new BigDecimal(btcAccountTransatcion.getAmount()));

        /**查询区块号**/
        String blocknum=btcWalletService.queryBlockByHash(btcAccountTransatcion.getBlockhash());
        if(StrUtil.isEmpty(blocknum)){
            log.warn("比特系查询根据区块hash查询区块号失败：hash="+btcAccountTransatcion.getBlockhash());
            throw new BizException();
        }
        walletDeposit.setBlockNum(Integer.parseInt(blocknum));

        walletDepositServiceImap.insert(walletDeposit);

        return  true;
    }



}
